<?php
namespace App\Lib\Amqp\Message;

use App\Lib\Consul\ServiceConfig;
use Swoft\Bean\BeanFactory;
abstract class AbstractMessage implements MessageInterface
{

    /**
     * MQ消息
     * @var
     */
    private $message;

    /**
     * 租户信息
     * @var string
     */
    protected $tenant = '';

    /**
     * 唯一 ID
     * @var string
     */
    protected $id = '';

    /**
     * 消息标题
     * @var string
     */
    protected $title = '';

    /**
     * 获取消息体
     * @var string
     */
    protected $content = '';

    /**
     * 发布者信息
     * @var string
     */
    protected $from = '';

    /**
     * 消费者信息
     * @var string
     */
    protected $to = '';

    /**
     * 发布时间
     * @var string
     */
    protected $publishTime = '';

    /**
     * 最后一次消费时间
     * @var string
     */
    protected $consumeTime = '';

    /**
     * 消费记录
     * @var array
     */
    protected $consumeLog = [];

    /**
     * 消费错误提示信息
     * @var string
     */
    protected $errorMsg = '';

    /**
     * MQ消息版本
     * @var float
     */
    protected $version = 1.0;

    /**
     * 获取通道
     * @var null
     */
    private $channel = null;

    /**
     * 交货标签
     * @var null
     */
    private $deliveryTag = null;

    /**
     * 获取源body
     * @var string
     */
    private $body = '';

    /**
     * 获取源数据
     * @var null
     */
    private $sourceData = null;

    /**
     * 消息体默认字段
     * @var array
     */
    private $messageFields = [
        'tenant',
        'id',
        'title',
        'content',
        'from',
        'to',
        'publishTime',
        'consumeTime',
        'consumeLog',
        'errorMsg',
        'version'
    ];

    /**
     * 获取租户信息
     * @return string
     */
    public function getTenant(): string
    {
        return $this->tenant;
    }

    /**
     * 设置租户信息
     * @param string $tenant
     * @return MessageInterface
     */
    public function setTenant($tenant = ''): MessageInterface
    {
        $this->tenant = $tenant;
        return $this;
    }

    /**
     * 获取消息ID
     * @return string
     */
    public function getID(): string
    {
        return $this->id;
    }

    /**
     * 设置消息ID
     * @param string $ID
     * @return MessageInterface
     */
    public function setID($ID): MessageInterface
    {
        $this->id = $ID;
        return $this;
    }

    /**
     * 获取消息标题
     * @return string
     */
    public function getTitle(): string
    {
        return $this->title;
    }

    /**
     * 设置消息标题
     * @param string $title
     * @return MessageInterface
     */
    public function setTitle($title = ''): MessageInterface
    {
        $this->title = $title;
        return $this;
    }

    /**
     * 获取消息体（Json格式）
     * @return string
     */
    public function getContent(): string
    {
        return $this->content;
    }

    /**
     * 设置消息体（Json）
     * @param string $content
     * @return MessageInterface
     */
    public function setContent($content = ''): MessageInterface
    {
        $this->content = $content;
        return $this;
    }

    /**
     * 获取发布消息源标识
     * @return string
     */
    public function getFrom(): string
    {
        return $this->from;
    }

    /**
     * 设置发布消息源标识
     * @param string $from
     * @return MessageInterface
     */
    public function setFrom($from): MessageInterface
    {
        $this->from = $from;
        return $this;
    }

    /**
     * 获取消费者信息
     * @return string
     */
    public function getTo(): string
    {
        return $this->to;
    }

    /**
     * 设置消费者信息
     * @param string $to
     * @return MessageInterface
     */
    public function setTo($to): MessageInterface
    {
        $this->to = $to;
        return $this;
    }

    /**
     * 获取发布时间
     * @return string
     */
    public function getPublishTime(): string
    {
        return $this->publishTime;
    }

    /**
     * 设置发布时间
     * @param $time
     * @return MessageInterface
     */
    public function setPublishTime($time): MessageInterface
    {
        $this->publishTime = $time;
        return $this;
    }

    /**
     * 获取消费时间
     * @return string
     */
    public function getConsumeTime(): string
    {
        return $this->consumeTime;
    }

    /**
     * 设置消费时间
     * @return MessageInterface
     */
    public function setConsumeTime($time): MessageInterface
    {
        $this->consumeTime = $time;
        return $this;
    }

    /**
     * 获取消费记录
     * @return array
     */
    public function getConsumeLog (): array
    {
        return $this->consumeLog;
    }

    /**
     * 设置消费记录
     * @param array $log
     * @return mixed
     */
    public function setConsumeLog (array $log): MessageInterface
    {
        $this->consumeLog = $log;
        return $this;
    }

    /**
     * 增加新的 消费记录
     * @param array $log
     * @return MessageInterface
     */
    public function addConsumeLog (array $log): MessageInterface
    {
        $consumeLog = $this->getConsumeLog();

        if (empty($log))
        {
            $log = [
                'order' => count($consumeLog) + 1,
                'consumeTime' =>  date('Y-m-d H:i:s', time()),
            ];
        }
        $consumeLog[] = $log;
        $this->consumeLog = $consumeLog;
        return $this;
    }

    /**
     * 获取错误提示
     * @return string
     */
    public function getErrorMsg (): string
    {
        return $this->errorMsg;
    }

    /**
     * 设置错误提示
     * @param string $errorMsg
     * @return MessageInterface
     */
    public function setErrorMsg ($errorMsg = ''): MessageInterface
    {
        $this->errorMsg = empty($errorMsg) ? "未知错误" : $errorMsg;
        return $this;
    }

    /**
     * 获取版本信息
     * @return string
     */
    public function getVersion (): string
    {
        return $this->version;
    }

    /**
     * 设置消息版本
     * @param int $version
     * @return MessageInterface
     */
    public function setVersion ($version = 0): MessageInterface
    {
        $this->version = $version === 0 ? $this->version : $version;
        return $this;
    }

    /**
     * 获取源body
     * @return mixed
     */
    public function getBody()
    {
        return $this->body;
    }

    /**
     * 设置源Body
     * @param string $body
     * @return mixed|void
     */
    public function setBody(string $body)
    {
        $this->body = $body;
    }

    /**
     * 获取源数据
     * @return mixed
     */
    public function getSourceData()
    {
        return $this->sourceData;
    }

    /**
     * 设置源数据
     * @param $sourceData
     * @return mixed|void
     */
    public function setSourceData($sourceData)
    {
        $this->sourceData = $sourceData;
    }

    /**
     * 转化成字符串格式
     * @return string
     */
    public function toString (): string
    {
        $result = [];
        foreach ($this->messageFields as $field) {
            $result[$field] = $this->$field;
        }
        return json_encode($result, JSON_UNESCAPED_UNICODE);
    }

    /**
     * 设置 Channel
     * @param $channel
     */
    public function setChannel ($channel) {
        $this->channel = $channel;
    }

    /**
     * 获取Channel
     * @return |null
     */
    public function getChannel () {
        return $this->channel;
    }

    /**
     * 获取交货标签
     * @return int|null
     */
    public function getDeliveryTag () {
        return $this->deliveryTag;
    }

    /**
     * 设置交货标签
     * @param int $deliveryTag
     */
    public function setDeliveryTag ($deliveryTag) {
        $this->deliveryTag = $deliveryTag;
    }

    /**
     * 创建默认的 租户信息
     */
    public function createDefaultTenant () {
        $tenant = getRpcTenant();
        $this->setTenant($tenant);
    }

    /**
     * 创建默认 ID 信息
     */
    protected function createDefaultUUID () {
        $UUID = uuid();
        $this->setID($UUID);
    }

    /**
     * 设置默认的发布者信息
     */
    protected function createDefaultFrom () {
        /** @var ServiceConfig $ServiceConfig */
        $ServiceConfig = BeanFactory::getBean("ServiceConfig");
        $this->setFrom($ServiceConfig->getName());
    }

    /**
     * 设置默认的发布时间
     */
    protected function createDefaultPublishTime () {
        $time = date('Y-m-d H:i:s', time());
        $this->setPublishTime($time);
    }

    /**
     * 设置默认的消费时间
     */
    protected function createDefaultConsumeTime () {
        $time = date('Y-m-d H:i:s', time());
        $this->setConsumeTime($time);
    }

}
